//
//  TZL_TabbarController.m
//  MyTabbarDemo
//
//  Created by Yuki on 2021/10/11.
//

#import "TZL_TabbarController.h"
#import "TZL_NavigationController.h"
#import "TZL_HomeController.h"
#import "TZL_MineController.h"

#import "TZL_PlusButton.h"

@interface TZL_TabbarController ()
/**  tabbarView  **/
@property (nonatomic , strong)UIView  *tabbarView;
@end

@implementation TZL_TabbarController

- (void)initWithWidow:(UIWindow *)window
{
    if (!window) {
        NSLog(@"window不能为空");
        return;
    }
    [TZL_PlusButton registerPlusButton];//注册中间发布按钮
    //配置tabbar
    CYLTabBarController *tabBarC = [CYLTabBarController tabBarControllerWithViewControllers:[self viewControllers] tabBarItemsAttributes:[self tabbarItemsAttributes] imageInsets:UIEdgeInsetsMake(-4, 0, 0, 0) titlePositionAdjustment:UIOffsetMake(0, -8)];
    
    [self customizeTabBarAppearance:tabBarC];
    
    
    self.tabbarView = [[UIView alloc]init];
    self.tabbarView.backgroundColor = UIColor.blackColor;
    [tabBarC.tabBar insertSubview:self.tabbarView atIndex:0];
    //这里的frame要和 CYLTabBarController.m的89行自己修改的部分的frame保持一致
    self.tabbarView.frame = CGRectMake(0, 0, UIScreen.mainScreen.bounds.size.width - 44, 55);
    window.rootViewController = tabBarC;
}


/// 控制器
- (NSArray *)viewControllers{
    //首页
    TZL_HomeController *home_vc = [[TZL_HomeController alloc]init];
    TZL_NavigationController *home_nav = [[TZL_NavigationController alloc]initWithRootViewController:home_vc];
    [home_vc cyl_setNavigationBarHidden:YES];
    
    //我的
    TZL_MineController *mine_vc = [[TZL_MineController alloc]init];
    TZL_NavigationController *mine_nav = [[TZL_NavigationController alloc] initWithRootViewController:mine_vc];
    
    //首页
    TZL_HomeController *home_vc2 = [[TZL_HomeController alloc]init];
    TZL_NavigationController *home_nav2 = [[TZL_NavigationController alloc]initWithRootViewController:home_vc2];
    [home_vc cyl_setNavigationBarHidden:YES];
    
    //我的
    TZL_MineController *mine_vc2 = [[TZL_MineController alloc]init];
    TZL_NavigationController *mine_nav2 = [[TZL_NavigationController alloc] initWithRootViewController:mine_vc2];
    
    return @[home_nav,mine_nav,home_nav2,mine_nav2];
}


/// item基本属性
- (NSArray *)tabbarItemsAttributes{
    NSDictionary *homeDic = @{
        CYLTabBarItemTitle :@"首页",
        CYLTabBarItemImage :@"tab_icon01a",
        CYLTabBarItemSelectedImage :@"tab_icon01b",
    };
    NSDictionary *mineDic = @{
        CYLTabBarItemTitle :@"我的",
        CYLTabBarItemImage :@"tab_icon04a",
        CYLTabBarItemSelectedImage :@"tab_icon04b"
    };
    return  @[homeDic,mineDic,homeDic,mineDic];
}

/// 配置tabbar的其他属性
/// @param tabBarC 待配置的tabbar
- (void)customizeTabBarAppearance:(CYLTabBarController *)tabBarC{
    
    [tabBarC hideTabBarShadowImageView];
    [tabBarC setTintColor:UIColor.blackColor];//设置主题色
    tabBarC.tabBarHeight = 55.0f;//tabbar的高度
    tabBarC.tabBar.layer.cornerRadius = 55.0f/2.0f; //切圆角
    tabBarC.tabBar.layer.masksToBounds = YES;
    //普通状态下的文字
    NSMutableDictionary *normalDic = [NSMutableDictionary new];
    normalDic[NSForegroundColorAttributeName] = ColorHex(0x787C8C, 1);
    normalDic[NSFontAttributeName] = [UIFont systemFontOfSize:10 weight:UIFontWeightMedium];
    //选中状态下的的文字
    NSMutableDictionary *selDic = [NSMutableDictionary new];
    selDic[NSForegroundColorAttributeName] = ColorHex(0xffffff, 1);
    selDic[NSFontAttributeName] = [UIFont systemFontOfSize:10 weight:UIFontWeightMedium];
    
    UITabBarItem *tabBar = [UITabBarItem appearance];
    [tabBar setTitleTextAttributes:normalDic forState:UIControlStateNormal];
    [tabBar setTitleTextAttributes:selDic forState:UIControlStateSelected];

   
}



/// 这里用了消息转发机制，在tzl_PlusButton里面转发的
- (void)didClickPlusBtn{
//    NSLog(@"-------这里实现了%@",NSStringFromClass(self.tabBarController.selectedViewController.class));
    //这里不能调用self，这里的self是TZL_PlusButton
    
    UIViewController *result = nil;
    // 获取默认的window
    UIWindow * window = [UIApplication sharedApplication].delegate.window;
    // app默认windowLevel是UIWindowLevelNormal，如果不是，找到它。
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows) {
            if (tmpWin.windowLevel == UIWindowLevelNormal) {
                window = tmpWin;
                break;
            }
        }
    }
    // 获取window的rootViewController
    result = window.rootViewController;
    while (result.presentedViewController) {
        result = result.presentedViewController;
    }
    if ([result isKindOfClass:[UITabBarController class]]) {
        result = [(UITabBarController *)result selectedViewController];
    }
    if ([result isKindOfClass:[UINavigationController class]]) {
        result = [(UINavigationController *)result visibleViewController];
    }
    
    NSLog(@"tzl_tabbarcontroller:%@",NSStringFromClass(result.class));
}

@end
